using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows.Input;
using TrackMe.Apps.Views;

namespace TrackMe.Apps.VM
{
    [Export]
	public class RecentTaskVM : BasicViewModel<IRecentView>, IActivatable
    {
        private readonly IDatabase database;
		
		private readonly ICommand selectTaskCmd;

    	[ImportingConstructor]
        public RecentTaskVM(IRecentView view, IDatabase database, [Import("SelectTask")] ICommand selectTaskCmd) : base(view)
        {
            this.database = database;
        	this.selectTaskCmd = selectTaskCmd;
        	LoadHistory();
        }

        public IList<ITaskForSelectVM> RecentTasks { get; private set; }

        public void LoadHistory()
        {
            RecentTasks = new List<ITaskForSelectVM>();
            foreach (var taskForSelect in database.RecentTasks(100))
            {
                var o = AutoIntf.New<ITaskForSelectVM>();
                o.Model = taskForSelect;
                o.SelectCmd = selectTaskCmd;
                RecentTasks.Add(o);
            }
            RaisePropertyChanged(() => RecentTasks);
        }

    	void IActivatable.OnActivate()
    	{
    		LoadHistory();
    	}
    }
}
